#include "CWorm.h"
#include "CCamera.h"
#include "..\\Wrappers\\CSGD_Direct3D.h"
#include "CPistolBullet.h"
#include "..\\Messaging\CSGP_MessageSystem.h"
#include "..\\Messaging\CSGP_EventSystem.h"
#include "../Wrappers/CSGD_TextureManager.h"

CWorm::CWorm(void)
{
	SetType(CBase::OBJ_WORM);

	SetWidth(32);
	SetHeight(32);

	isJumping = false;
	speed = 75;
	jumpRange = 100;
	jumpSpeed = 300;
	jumpTime = 0.0f;
	jumpLength = 1.0f;
	damage = 15;

	ImageID = CSGD_TextureManager::GetInstance()->LoadTexture("Resource/Graphics/worm.png");
}

CWorm::~CWorm(void)
{

}

void CWorm::Fire(void)
{

}

/*
1) chase player
2) when within strike distance jump in a straight line toward player
3) if it misses, 1)
3) if it hits do damage to the player

*/

void CWorm::Update(float fElapsedTime)
{
	CEnemy::Update(fElapsedTime);
	tVector2D tTemp;
		tTemp.fX = CPlayer::GetInstance()->GetPosX() - GetPosX();
		tTemp.fY = CPlayer::GetInstance()->GetPosY() - GetPosY();

	if(!isJumping)
	{
		

		if(Vector2DLength(tTemp) <= jumpRange)
		{
			Jump(Vector2DNormalize(tTemp));
			return;
		}

		tTemp = Vector2DNormalize(tTemp);

		SetVelX(tTemp.fX*speed);
		SetVelY(tTemp.fY*speed);
	}
	else
	{
		jumpTime += fElapsedTime;
		if(jumpTime >= jumpLength)
		{
			isJumping = false;
			jumpTime = 0.0f;
		}
	}

	if(tTemp.fX >0)
	{
		
		IsBack = true;
	}
	else 
		IsBack = false;
	
}

void CWorm::Jump(tVector2D vec)
{
	SetVelX(vec.fX*jumpSpeed);
	SetVelY(vec.fY*jumpSpeed);
	isJumping = true;
}

void CWorm::Render(void)
{
	RECT drawRect = GetCollisionRect();
	drawRect.right -= CCamera::GetInstance()->GetOffsetX();
	drawRect.left -= CCamera::GetInstance()->GetOffsetX();
	drawRect.top -= CCamera::GetInstance()->GetOffsetY();
	drawRect.bottom -= CCamera::GetInstance()->GetOffsetY();
	// Show collision box
	//CSGD_Direct3D::GetInstance()->DrawRect(drawRect, 255, 0, 255);

	if(IsBack)
		CSGD_TextureManager::GetInstance()->Draw(ImageID,drawRect.left +(drawRect.right - drawRect.left),drawRect.top,-1.0f,1.0f);
	else
		CSGD_TextureManager::GetInstance()->Draw(ImageID,drawRect.left ,drawRect.top,1.0f,1.0f);

	// Draw health above enemies head
	//char buffer[128];
	//sprintf_s(buffer, _countof(buffer), "HP: %i", GetHealth());

	//CSGD_Direct3D::GetInstance()->DrawText(buffer, (int)GetPosX(), (int)GetPosY() - 20, 255, 0, 0);
}

bool CWorm::CheckCollision(IBaseInterface* IBase)
{
	if(CEnemy::CheckCollision(IBase))
		return true;
	//if(IBase->GetType() == OBJ_PISTOL_BULLET)
	//{
	//	CPistolBullet* tempb = (CPistolBullet*) IBase;
	//	if(tempb->GetBulletType() == CPistolBullet::BLT_PLAYER)
	//	{
	//		RECT tempr;
	//		if(IntersectRect(&tempr, &this->GetRect(), &tempb->GetCollisionRect()))
	//		{
	//			SetHealth(GetHealth() - 5);
	//			CSGP_MessageSystem::GetInstance()->SendMessageA(new CDestroyPlayerBulletMessage(tempb));
	//			return true;
	//		}
	//	}
	//}

	return false;
}